package com.loadmore.oneday.fragment

import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.loadmore.common.base.BaseDecoration
import com.loadmore.common.base.BaseFragment
import com.loadmore.common.constants.Api
import com.loadmore.common.model.BaseResponse
import com.loadmore.common.model.Like
import com.loadmore.common.model.Reply
import com.loadmore.common.model.Topic
import com.loadmore.common.module.reply.ReplyAdapter
import com.loadmore.common.net.Request
import com.loadmore.common.net.impl.JsonCallback
import com.loadmore.common.router.RouterPath
import com.loadmore.common.variables.Variables
import com.loadmore.oneday.R
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.fragment_topic_detail.*

/**
 * @author sunleilei
 * @date 2018/7/19
 * @time 11:13
 */

@Route(path = RouterPath.TopicDetail)
class TopicDetailFragment : BaseFragment() {
    lateinit var adapter:ReplyAdapter
    val current_page = 1
    override fun getLayoutId(): Int {
        return R.layout.fragment_topic_detail
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapter = ReplyAdapter(context!!)
        reply_list.layoutManager  = androidx.recyclerview.widget.LinearLayoutManager(context)
        reply_list.addItemDecoration(BaseDecoration(context!!))
        reply_list.adapter = adapter
        reply_list.isNestedScrollingEnabled = false
        var topic: Topic = arguments?.get("data") as Topic
        if (topic!= null) {
            content.text = topic.body
            getReply(topic.getId()!!.toString())

            like.setOnClickListener {
                if (topic.like) {
                    unLike(topic.id)
                }else {
                    like(topic.id)
                }
            }
        }

    }

    private fun getReply(id:String) {
        var params = HashMap<String, Any>()
        params.put("with_page",true)
        params.put("current_page",current_page)
        Request.get(Api.reply, hashMapOf<String,Any>("post_id" to id,"token" to Variables.token!!),object : JsonCallback<BaseResponse<List<Reply>>>(){
            override fun onSuccess(response: Response<BaseResponse<List<Reply>>>?) {
                val data:List<Reply>? = response?.body()?.data
                if (data != null) {
                    adapter.append(data)
                }
            }

            override fun onError(response: Response<BaseResponse<List<Reply>>>?) {
                super.onError(response)
            }


        })
    }


    private fun like(id:Long) {
        var params:HashMap<String,Any> = hashMapOf()
        params.put("post_id",id)
        params.put("token", Variables.token!!)
        Request.post(Api.like,params,object : JsonCallback<BaseResponse<Like>>() {
            override fun onSuccess(response: Response<BaseResponse<Like>>?) {
                Toast.makeText(context,"like success",Toast.LENGTH_SHORT).show()
            }

            override fun onError(response: Response<BaseResponse<Like>>?) {
                super.onError(response)
            }

        })
    }

    private fun unLike(id:Long) {
        var params:HashMap<String,Any> = hashMapOf()
        params.put("post_id",id)
        params.put("token", Variables.token!!)
        Request.delete(Api.like,params,object : JsonCallback<BaseResponse<Like>>() {
            override fun onSuccess(response: Response<BaseResponse<Like>>?) {
                Toast.makeText(context,"unlike success", Toast.LENGTH_SHORT).show()
            }

            override fun onError(response: Response<BaseResponse<Like>>?) {
                super.onError(response)
            }

        })
    }
}
